package org.chen.org.controller;

import org.chen.org.domain.Employee;
import org.chen.org.service.IShopEmployeeService;
import org.chen.org.service.IShopService;
import org.chen.org.domain.Shop;
import org.chen.org.query.ShopQuery;
import org.chen.bas.util.AjaxResult;
import org.chen.bas.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;

import javax.mail.internet.MimeMessage;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private IShopEmployeeService shopEmployeeService;
    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.insert(shop);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete (@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Shop shop = shopService.selectById(id);
            return AjaxResult.success(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Shop> list = shopService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody ShopQuery query)
    {
        try {
            PageInfo<Shop> pageInfo = shopService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }
    /**
    * 批量删除
    * @return
    */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){

        try {
              shopService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！"+e.getMessage());
        }
    }
    @PostMapping("settlement")
    public AjaxResult settlement(@RequestBody Shop shop){

        try {
            shopService.settlement(shop);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！"+e.getMessage());
        }
    }

    @PutMapping("/detection")
    public AjaxResult detection(@RequestBody Shop shop){
        try {
            shopService.update1(shop);
            Employee employee = shopEmployeeService.select1(shop);
            shop.setOperate_id(employee.getId());
            shopService.insert1(shop);
            String e = employee.getEmail();
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1144027327@qq.com");
            helper.setText("<h1>恭喜你，店铺审核通过，点击下方链接去激活</h1>"+
                    "<a href=\"http://127.0.0.1:8090/#/Regshop\">点我激活！！！</a>",true);
            //添加附件
            //收件人
            helper.setTo(e);
            javaMailSender.send(mimeMessage);

            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody Shop shop){
        try {
            shopService.update2(shop);
            Employee employee = shopEmployeeService.select1(shop);
            System.out.println(employee.getId());
            shop.setOperate_id(employee.getId());
            shopService.insert2(shop);
            String e = employee.getEmail();
            String n = shop.getNote();
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1144027327@qq.com");
            helper.setText("当前请求被驳回，驳回理由是："+n+"<a href=\"http://127.0.0.1:8090/#/Regshop\">点我重新注册</a>",true);
            //添加附件
            //收件人
            helper.setTo(e);
            javaMailSender.send(mimeMessage);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }

    @PostMapping("/active")
    public AjaxResult active(@RequestParam(name = "eid", required = false) String eid,
                             @RequestParam(name = "sid", required = false) String sid)
    {
        try {
            System.out.println(eid);
            System.out.println(sid);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("商户入驻失败！"+e.getMessage());
        }
    }
}
